package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Txl;

/**
 * A data access object (DAO) providing persistence and search support for Txl
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fick.hibernate.dao.Txl
 * @author MyEclipse Persistence Tools
 */

public class TxlDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TxlDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String TEL = "tel";
	public static final String PHONE = "phone";
	public static final String BZ = "bz";
	public static final String ADMIN_ID = "adminId";

	public void attachClean(Txl instance) {
		log.debug("attaching clean Txl instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Txl instance) {
		log.debug("attaching dirty Txl instance");
		try {
			Transaction ts = getSession().beginTransaction();
			getSession().saveOrUpdate(instance);
			ts.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public void delete(Txl persistentInstance) {
		log.debug("deleting Txl instance");
		try {
			Transaction ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findAll() {
		log.debug("finding all Txl instances");
		try {
			String queryString = "from Txl";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByAdminId(Object adminId) {
		return findByProperty(ADMIN_ID, adminId);
	}

	public List findByBz(Object bz) {
		return findByProperty(BZ, bz);
	}

	public List findByExample(Txl instance) {
		log.debug("finding Txl instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Txl").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Txl findById(java.lang.Integer id) {
		log.debug("getting Txl instance with id: " + id);
		try {
			Txl instance = (Txl) getSession().get("com.fick.hibernate.model.Txl", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Txl instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Txl as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public Txl merge(Txl detachedInstance) {
		log.debug("merging Txl instance");
		try {
			Txl result = (Txl) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}